home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5301 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.MWCI.NET!usenet
  2. From: stuffle@pcii.net
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why Do I Use An Ampersand in Member Class Parameters?
  5. Date: 3 Feb 1996 16:48:57 GMT
  6. Organization: MidWest Communications, Inc.
  7. Message-ID: <4f03lq$gv7@hihat.mwci.net>
  8. References: <4emnv2$n5o@alcor.usc.edu>
  9. Reply-To: stuffle@pcii.net
  10. NNTP-Posting-Host: lan-pm1-5.pcii.net
  11. X-Newsreader: IBM NewsReader/2 v1.2.5
  12.  
  13. In <4emnv2$n5o@alcor.usc.edu>, wawda@alcor.usc.edu (Abu Wawda) writes:
  14. >class Simple
  15. >{
  16. >public:
  17. >  Simple();
  18. >  int operator += (const Simple &);
  19. >private:
  20. >  int data;
  21. >};
  22. >
  23. >I have seen many examples of this but I what I don't understand is why
  24. >there is an amersand after Simple? I would wind up implementating the
  25. >function as something like the following:
  26. >
  27.  
  28. This is passing by reference, and it is similar to passing a pointer in so far that
  29. you are not passing the actual object, but its address.
  30.  
  31. Passing by reference is safer than passing a pointer.  You cannot do things like
  32. delete (or free) the memory, or reassign "parameter" to point to some other
  33. area of memory.
  34.  
  35. I like to think of this as being similar to:
  36.  
  37. function (VAR x : SomeType) return INTEGER;  in Pascal.
  38.  
  39. >int Simple::operator += (const Simple ¶meter)
  40. >{
  41. >  data += parameter.data;
  42. >}
  43. >
  44. >I mean, I would understand if I parameter were a pointer to a Simple
  45. >class, but that is not the case, since I am not doing:
  46. >
  47. >int Simple::operator += (const Simple ¶meter)
  48. >{
  49. >  data += parameter->data;
  50. >}
  51. >
  52. >Then why is there an amersand? I would appreciate any
  53. >suggestions. Thank you!
  54. >
  55. >
  56. >-Abu Wawda
  57. > wawda@scf.usc.edu
  58. >
  59. >
  60.  
  61.  
  62.                                 \\\|///
  63.                                 | ~ ~ |
  64.                                (- @ @ -)
  65. //------------------------oOOo----(_)----oOOo------------------------
  66. //  Ken Sodemann  (Stuffle)             |   Get Warped!!!!
  67. //  Stuffle@PCII.NET                    |    
  68. //  http://users.mwci.net/~stuffle/     |   
  69. //-------------------------------------------------------------------
  70.  
  71.